﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entidades;
using MySql.Data.MySqlClient;

namespace Datos
{
    public class MySqlTaxiDAO:ITaxi
    {

        MySqlConnection conector = new MySqlConnection();
        string connectionString;
        public string s;

        public void iniciarConexion()
        {
          
            try
            {
                connectionString = "Server=127.0.0.1;Database=justime;Uid=root; pwd=root;";
                conector.ConnectionString = connectionString;
                conector.Open();
                s = "La conexion se ha realizado con exito";

            }
            catch
            {
                s = "Ocurrio un error al intentar conectarse";
            }

        }

        public void Conectar()
        {
            iniciarConexion();
        }

        public void Desconectar()
        {
            conector.Close();
        }

        public List<Taxi> getTodosActivos() // probar q funcione
        {
            List<Taxi> lst = new List<Taxi>();
            MySqlCommand cmd = conector.CreateCommand();
            cmd.CommandText = "Select * From taxi where estado='1'";
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Taxi c = new Taxi();
                c.Marca = reader["marca"].ToString();
                c.Placa = reader["placa"].ToString();
                c.conductor.Email = reader["email"].ToString();              
                lst.Add(c);
            }
            return lst;
        }

        public Taxi getTaxi(string email)
        {
            Taxi taxi = new Taxi();
            MySqlCommand cmd = conector.CreateCommand();
            cmd.CommandText = "Select * From taxi where email=?email";
            cmd.Parameters.AddWithValue("?email", email);
            MySqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read()) {
                taxi.Marca = reader["marca"].ToString();
                taxi.Placa = reader["placa"].ToString();
                taxi.Estado = reader["estado"].ToString();
                taxi.Color = reader["color"].ToString();
            }
            return taxi;
        }
    }
}
